<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 22/10/14
 * Time: 1:26 PM
 */
use Laravel\Input;

class Site_Profile_Controller extends Base_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("default") ;
		$this->theme->set_layout('column2');
	}

	public function get_index(){

		$id =  end(URI::$segments);

		if(!empty($id)){
			$post = Post::find($id);
			$data['post'] = $post;

			$data['mode'] = "LISTING";
			//--Meta Data --//
			$this->theme->title($post->meta_title.' - '.Settings::get('en','site_title'));
			$this->theme->keyword($post->meta_keyword);
			$this->theme->description($post->meta_description);

			//--MAP--//
			if($post->lat && $post->long){
				$config = array();
				$long_lat = $post->lat.','.$post->long;

				$config['minifyJS'] = TRUE;
				$config['map_type'] = 'ROADMAP';
				$config['trafficOverlay'] = 'TRUE';
				$config['map_height'] = '300px';
				$config['zoom'] = "13";
				$config['center'] = $long_lat;

				$config['adsense'] = FALSE;
				$config['adsensePublisherID'] = Settings::get('en','adsense_pub_id');
				$config['adsenseFormat'] = 'HALF_BANNER';
				$config['adsensePosition'] = 'BOTTOM_LEFT';

				$Gmap = new Gmaps();
				$Gmap->initialize($config);
				$marker = array();
				$marker['icon'] = \Laravel\URL::base()."/public/pointer1.png";
				$marker['position'] = $long_lat;
				$marker['infowindow_content'] = "<strong>".$post->title."</strong>"."</br>".$post->address1;
				$marker['animation'] = 'DROP';
				$Gmap->add_marker($marker);

				$data['map'] = $Gmap->create_map();

				 
				/*-- BREADCRUMB */

				
				;
				

				$cat = Categories::find(Terms::category($data['post']->id,true));
				$loc = Locations::find(Terms::location($data['post']->id,true))->name;
				$catUrl = Str::slug(Location::get()).'/'.$cat->slug;
				
				Breadcrumb::addbreadcrumb('Home', URL::base());
				Breadcrumb::addbreadcrumb($loc, URL::base().'/'.$loc);
				Breadcrumb::addbreadcrumb($cat->name, URL::base().'/'.$catUrl);
				Breadcrumb::addbreadcrumb($data['post']->title, URL::base());
				
				
				Breadcrumb::setSeparator('&raquo;');
				$data['breadcrumb'] = Breadcrumb::generate();
				
				$data['photos'] = DB::table('gallery')->where('post_id','=',$data['post']->id)->where('term','=','photo')->get();
				$data['videos'] = DB::table('gallery')->where('post_id','=',$data['post']->id)->Where('term','=', 'video')->get();
				
				$data['comments'] = Comments::where('post_id','=',$data['post']->id)->get();
				
				$ratting = Comments::rattig($data['post']->id);
				
				$data['post_ratting'] = substr($ratting, 0,1);

				$data['ratting'] = Config::get('Comments::config.ratting');
				
				$data['report'] = Config::get('Site::config.report');
				
				

			}
			return $this->theme->render('site::profile',$data);
		}
	}

	

	
	
	public function post_enquiry(){

		
		$rules = array(
				'enquiry_name' => 'required|min:4',
				'enquiry_email' => 'required|email',
				'enquiry_contact' => 'required',
				'enquiry_description' => 'required|min:25'
		);

		$loc = Input::get('selectbox-mylocation1');
			
		$loca = Locations::where('id','=',$loc)->first()->name;
		
		$v = Validator::make(Input::all(), $rules);

		if ($v->fails()) {
			$response = array(
					'status' => 'Fail',
					'message' => '<span class="text text-danger">Invalid Input..</span>',
			);
		} else {
			
			
			
			$data = array(
					'post_id' => Input::get('post_id'),
					'name' => trim(Input::get('enquiry_name')),
					'email' => trim(Input::get('enquiry_email')),
					'contact' => trim(Input::get('enquiry_contact')),
					'location' => $loca,
					'description' => Input::get('enquiry_description'),
					'type' => 'ENQ',
					'created_on' => Commoncall::current_timestamp(),
					'ip_address' => $_SERVER["REMOTE_ADDR"],
			);

			if (DB::table('report')->insert($data)) {

				
				$emailTemp = DB::table('emailtemplate')->where('variable','=','send_enquiry_mail')->first();
				
				
				$temTitle = $emailTemp->title;
				$temDesc = $emailTemp->description;
				$postTitle = Input::get('post_id');
				
				$postTitle = Post::where('id','=',$postTitle)->first()->title;
				
				$postUrl = Input::get('listing_url');
				
				$webTitle = Settings::get('en','site_title');
				$body = array(
						'post_title' => "<a href='".$postUrl."'>$postTitle</a>",
						'website_title' => "<a href='".URL::base()."'>$webTitle</a>",
						'name' => trim(Input::get('enquiry_name')),
						'email' => trim(Input::get('enquiry_email')),
						'contact_no' => trim(Input::get('enquiry_contact')),
						'location' => $loca,
						'content' => Input::get('enquiry_description'),
				);
				
				
				$footer = Settings::get('en','mail_smtp_footer');
				
				
				$description = Parser::parse_string($temDesc, $body,TRUE);
				
				$description .= "<p>$footer</p>";
			
				$p = array(
						'from' => trim(Input::get('enquiry_email')),
						'to' => trim(Input::get('owner_email')),
						'sender_name' => trim(Input::get('enquiry_name')),
						'reply_to' => 'noreply@gmail.com',
						'subject' => trim($temTitle),
						'body' => trim($description),
						
				);
				
				//dd($p);
				Commoncall::mail($p);
				
				$response = array(
						'status' => 'Success',
						'message' => '<span class="text text-info">Thanks! Enquiry sent...</span>',
				);

				//--send mail to Business Owner --//

			} else {
				$response = array(
						'status' => 'Fail',
						'message' => '<span class="text text-danger">Ah! something went wrong..</span>',
				);
			}
		}

		return Response::json($response);
	}
	
	public function post_inc()
	{
		 
		 
			
		//check if its our form
	
		$rules = array(
				'report_name' => 'required|min:4',
				'report_email' => 'required|email',
				'report_contact' => 'required',
				'report_type' => 'required',
				'report_description' => 'required|min:25'
		);
		
		$loc = Input::get('selectbox-mylocation3');
			
		$loca = Locations::where('id','=',$loc)->first()->name;
		
	
		$v = Validator::make(Input::all(), $rules);
	
		if ($v->fails()) {
			
			
			 
			$response = array(
					'status' => 'Fail',
					'message' => '<span class="text text-danger">Invalid Input..</span>',
			);
		} else {
			 
			
			$data = array(
					'post_id' => Input::get('post_id'),
					'name' => trim(Input::get('report_name')),
					'email' => trim(Input::get('report_email')),
					'contact' => trim(Input::get('report_contact')),
					'location' => $loca,
					'type' => 'INQ',
					'title' => Input::get('report_type'),
					'description' => Input::get('report_description'),
					'created_on' => Commoncall::current_timestamp(),
					'ip_address' => $_SERVER["REMOTE_ADDR"],
			);
	
			  //dd($data);
	
			if (DB::table('report')->insert($data)) {
	
				//--Send Mail to Administrator--//
				
				$emailTemp = DB::table('emailtemplate')->where('variable','=','report_incorrect')->first();
				
				
				$temTitle = $emailTemp->title;
				$temDesc = $emailTemp->description;
				$postTitle = Input::get('post_id');
				
				$postTitle = Post::where('id','=',$postTitle)->first()->title;
				
				$postUrl = Input::get('listing_url1');
				
				$webTitle = Settings::get('en','site_title');
				$body = array(
						'post_title' => "<a href='".$postUrl."'>$postTitle</a>",
						'website_title' => "<a href='".URL::base()."'>$webTitle</a>",
						'name' => trim(Input::get('report_name')),
						'email' => trim(Input::get('report_email')),
						'contact_no' => trim(Input::get('report_contact')),
						'location' => $loca,
						'report_type' => Input::get('report_type'),
						'content' => Input::get('report_description'),
				);
				
				
				$footer = Settings::get('en','mail_smtp_footer');
				
				
				$description = Parser::parse_string($temDesc, $body,TRUE);
				
				$description .= "<p>$footer</p>";
					
				$p = array(
						'from' => trim(Input::get('report_email')),
						'to' => trim(Settings::get('en','default_email')),
						'sender_name' => trim(Input::get('report_name')),
						'reply_to' => 'noreply@gmail.com',
						'subject' => trim($temTitle),
						'body' => trim($description),
				
				);
				
				Commoncall::mail($p);
					
				//--//
				
				//--Send Mail to Comment Poster --//
				
				//    Commoncall::mail($parameter,null,null);
	
	
	
	
	
				$response = array(
						'status' => 'Success',
						'message' => '<span class="text text-info">Thanks! Report Incorrect Post</text>',
				);
	
				//  return Redirect::back();
				//--send mail to Business Owner --//
	
	
	
	
	
			} else {
				$response = array(
						'status' => 'Fail',
						'message' => '<span class="text text-danger">Ah! something went wrong..</text>',
				);
			}
			
			
			
		}
	
		return Response::json($response);
		 
	}
		
	
	public function post_claim(){
	
	
		$rules = array(
				'claim_name' => 'required|min:4',
				'claim_email' => 'required|email',
				'claim_contact' => 'required',
				'claim_description' => 'required|min:25'
		);
	
		$loc = Input::get('selectbox-mylocation4');
			
		$loca = Locations::where('id','=',$loc)->first()->name;
		
		$v = Validator::make(Input::all(), $rules);
	
		if ($v->fails()) {
			$response = array(
					'status' => 'Fail',
					'message' => '<span class="text-danger">Invalid Input..</span>',
			);
		} else {
			$data = array(
					'post_id' => Input::get('post_id'),
					'name' => trim(Input::get('claim_name')),
					'email' => trim(Input::get('claim_email')),
					'contact' => trim(Input::get('claim_contact')),
					'location' => $loca,
					'description' => Input::get('claim_description'),
					'type' => 'CLAIM',
					'created_on' => Commoncall::current_timestamp(),
					'ip_address' => $_SERVER["REMOTE_ADDR"],
			);
	
			if (DB::table('report')->insert($data)) {
	
	
				$emailTemp = DB::table('emailtemplate')->where('variable','=','claim_of_business')->first();
				
				
				$temTitle = $emailTemp->title;
				$temDesc = $emailTemp->description;
				$postTitle = Input::get('post_id');
				
				$postTitle = Post::where('id','=',$postTitle)->first()->title;
				
				$postUrl = Input::get('listing_url2');
				
				$webTitle = Settings::get('en','site_title');
				$body = array(
						'post_title' => "<a href='".$postUrl."'>$postTitle</a>",
						'website_title' => "<a href='".URL::base()."'>$webTitle</a>",
						'name' => trim(Input::get('claim_name')),
						'email' => trim(Input::get('claim_email')),
						'contact_no' => trim(Input::get('claim_contact')),
						'location' => $loca,
						'position_at' => trim(Input::get('claim_position_at')), 
						'content' => Input::get('claim_description'),
				);
				
				
				
				$footer = Settings::get('en','mail_smtp_footer');
				
				
				$description = Parser::parse_string($temDesc, $body,TRUE);
				
				$description .= "<p>$footer</p>";
			
				$p = array(
						'from' => trim(Input::get('claim_email')),
						'to' => trim(Settings::get('en','default_email')),
						'sender_name' => trim(Input::get('claim_name')),
						'reply_to' => 'noreply@gmail.com',
						'subject' => trim($temTitle),
						'body' => trim($description),
						
				);
				
				Commoncall::mail($p);
			
	
				$response = array(
						'status' => 'Success',
						'message' => '<span class="text-info">Thanks! Claim Report sent...</span>',
				);
	
				//--send mail to Business Owner --//
	
			} else {
				$response = array(
						'status' => 'Fail',
						'message' => '<span class="text-danger">Ah! something went wrong..</span>',
				);
			}
		}
	
		return Response::json($response);
	}
	

}